package com.epfresh.basedialog;

import android.app.Activity;
import android.app.Dialog;
import android.content.Intent;
import android.support.annotation.NonNull;
import android.text.TextUtils;
import android.view.View;
import android.widget.TextView;

import com.epfresh.R;
import com.epfresh.api.helper.ApplicationHelper;
import com.epfresh.api.utils.AppUtils;
import com.epfresh.api.utils.T;
import com.umeng.socialize.PlatformConfig;
import com.umeng.socialize.ShareAction;
import com.umeng.socialize.UMShareAPI;
import com.umeng.socialize.UMShareListener;
import com.umeng.socialize.bean.SHARE_MEDIA;
import com.umeng.socialize.media.UMImage;
import com.umeng.socialize.media.UMWeb;

/**
 * Author: Chen
 * Date: 2019/4/23
 * Desc:
 */
public class NewShareDialog extends Dialog implements View.OnClickListener{
    private Activity activity;

    private String targetUrl;
    private String imgUrl;
    private String text;
    private String title;

    public NewShareDialog(@NonNull Activity context) {
        super(context, R.style.no_title_dialog);
        activity = context;
        setContentView(R.layout.layout_share_new);
        initView();
    }

    private void initView() {
        View rl_weChat = findViewById(R.id.rl_weChat);
        View rl_weChat_circle = findViewById(R.id.rl_weChat_circle);
        View rl_copy_link = findViewById(R.id.rl_copy_link);
        TextView txt_cancel = findViewById(R.id.txt_cancel);

        rl_weChat.setOnClickListener(this);
        rl_weChat_circle.setOnClickListener(this);
        rl_copy_link.setOnClickListener(this);
        txt_cancel.setOnClickListener(this);
    }

    public void showDialog(String targetUrl, String imgUrl, String text, String title) {
        this.targetUrl = targetUrl;
        this.imgUrl = imgUrl;
        this.text = text;
        this.title = title;
        super.show();
    }


    @Override
    public void onClick(View v) {
        dismiss();
        if (v.getId() == R.id.rl_copy_link) {
            if (targetUrl == null)
                targetUrl = "";
            copyLink(targetUrl);
        } else {
            share(v.getId());
        }
    }

    private void copyLink(String targetUrl) {
        AppUtils.copy(targetUrl, getContext());
        if (activity != null)
            T.toast("复制网址成功");
    }

    private void share(int id) {
        UMImage image = null;
        if (!TextUtils.isEmpty(imgUrl)) {
            image = new UMImage(activity, imgUrl);
        }

        UMWeb umWeb = new UMWeb(targetUrl,title,text,image);
        switch (id) {
            case R.id.rl_weChat:
                new ShareAction(activity)
                        .setPlatform(SHARE_MEDIA.WEIXIN).setCallback(umShareListener)
                        .withMedia(umWeb)
                        .share();
                break;
            case R.id.rl_weChat_circle:
                new ShareAction(activity)
                        .setPlatform(SHARE_MEDIA.WEIXIN_CIRCLE).setCallback(umShareListener)
                        .withMedia(umWeb)
                        .share();
                break;
            default:
                break;
        }
    }

    private UMShareListener umShareListener = new UMShareListener() {
        @Override
        public void onStart(SHARE_MEDIA share_media) {

        }

        @Override
        public void onResult(SHARE_MEDIA platform) {
            if (activity != null)
                T.toast(ApplicationHelper.getInstance().getDoc().getShared());
        }

        @Override
        public void onError(SHARE_MEDIA platform, Throwable t) {
            if (activity != null)
                T.toast("分享失败");
        }

        @Override
        public void onCancel(SHARE_MEDIA platform) {
//            if (activity != null)
//                T.toast("分享取消");
        }
    };

    public void onResult(int requestCode, int resultCode, Intent data) {
        if (activity != null)
            UMShareAPI.get(activity).onActivityResult(requestCode, resultCode, data);
    }
}
